package de.justphil.tcg.tcgserver.rest.db.mapstores;


import static de.justphil.tcg.tcgserver.commons.logging.TCGLogging.restLogger;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.TypedQuery;

import org.slf4j.Logger;

import com.hazelcast.core.MapStore;

import de.justphil.tcg.tcgserver.commons.domain.CardInstance;
import de.justphil.tcg.tcgserver.commons.management.Application;
import de.justphil.tcg.tcgserver.rest.db.mapstores.helpers.LoadHelper;

public class CardInstanceMapStore implements MapStore<Long, CardInstance> {

	private static final Logger log = restLogger(CardInstanceMapStore.class);
	
	private final EntityManagerFactory factory;

	public CardInstanceMapStore() {
		log.debug("New object created!");
		factory = Application.getEntityManagerFactory();
	}
	
	@Override
	public CardInstance load(Long ciId) {
		log.debug("load()");
		
		EntityManager em = factory.createEntityManager();
		
		CardInstance ci = em.find(CardInstance.class, ciId);
		loadTransientFields(em, ci);
		
		em.close();
		
		return ci;
	}

	@Override
	public Map<Long, CardInstance> loadAll(Collection<Long> ciIds) {
		log.debug("loadAll()");
		
		EntityManager em = factory.createEntityManager();
		Map<Long, CardInstance> ciMap = new HashMap<Long, CardInstance>();
		
		List<CardInstance> ciList = getCardInstanceList(em);
		for(CardInstance ci : ciList) {
			loadTransientFields(em, ci);
			
			log.debug("CardInstance {} is part of {} deck(s).", ci.getId(), ci.getDecks().size());
			
			ciMap.put(ci.getId(), ci);
		}
		
		em.close();
		
		return ciMap;
	}

	@Override
	public Set<Long> loadAllKeys() {
		log.debug("loadAllKeys()");
		
		EntityManager em = factory.createEntityManager();
		
		Set<Long> ciIds = new HashSet<Long>();
		
		List<CardInstance> ciList = getCardInstanceList(em);
		for (CardInstance ci : ciList) {
			ciIds.add(ci.getId());
		}
		
		em.close();
		
		return ciIds;
	}
	
	private void loadTransientFields(EntityManager em, CardInstance ci) {
		if (ci == null) {
			return;
		}
		
		LoadHelper.loadIncludingDecks(em, ci);
	}
	
	private List<CardInstance> getCardInstanceList(EntityManager em) {
		TypedQuery<CardInstance> q = em.createQuery("SELECT c FROM CardInstance c ORDER BY c.id", CardInstance.class);
		return q.getResultList();
	}

	@Override
	public void delete(Long ciId) {
		log.debug("delete()");
		
		EntityManager em = factory.createEntityManager();
		em.getTransaction().begin();
		
		CardInstance ci = em.find(CardInstance.class, ciId);
		if (ci != null) {
			em.remove(ci);
		}
		
		em.getTransaction().commit();
		em.close();
	}

	@Override
	public void deleteAll(Collection<Long> ciIds) {
		log.debug("deleteAll()");
		
		EntityManager em = factory.createEntityManager();
		em.getTransaction().begin();
		
		CardInstance ci = null;
		for (Long ciId : ciIds) {
			ci = em.find(CardInstance.class, ciId);
			if (ci != null) {
				em.remove(ci);
			}
		}
		
		em.getTransaction().commit();
		em.close();
	}

	@Override
	public void store(Long ciId, CardInstance ci) {
		log.debug("store()");
		
		EntityManager em = factory.createEntityManager();
		em.getTransaction().begin();
		
		CardInstance pCi = em.find(CardInstance.class, ciId);
		if (pCi == null) {
			log.debug("em.persist()");
			em.persist(ci);
		}
		else {
			log.debug("em.merge()");
			em.merge(ci);
		}
		
		em.getTransaction().commit();
		em.close();
	}

	@Override
	public void storeAll(Map<Long, CardInstance> ciMap) {
		log.debug("storeAll()");
		
		EntityManager em = factory.createEntityManager();
		em.getTransaction().begin();
		
		CardInstance pCi	= null;
		CardInstance ci		= null;
		for (Long ciId : ciMap.keySet()) {
			ci = ciMap.get(ciId);
			
			pCi = em.find(CardInstance.class, ciId);
			if (pCi == null) {
				log.debug("em.persist()");
				em.persist(ci);
			}
			else {
				log.debug("em.merge()");
				em.merge(ci);
			}
		}
		
		em.getTransaction().commit();
		em.close();
	}

}
